<template>
  <div class='container'>
    <el-card style="margin-bottom: 20px;">
      <!-- 头 -->
      <el-row :gutter="20">
        <el-col :span="16">
          <!-- 右边表单 -->
          <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="关键字">
            <el-input v-model="formInline.keyword"></el-input>
          </el-form-item>
          <el-form-item label="状态">
           <el-select v-model="formInline.state" placeholder="请选择">
              <el-option label="启用" value="0"></el-option>
              <el-option label="禁用" value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button size="small" @click='clear'>清除</el-button>
            <el-button type="primary" size="small" @click="search">搜索</el-button>
          </el-form-item>
          </el-form>

        </el-col>
        <el-col :span="8">
          <!-- 左边按钮 -->
          <el-row type="flex" justify="end">
            <el-button type="success" size="medium" class="el-icon-edit" @click="newadd">新增技巧</el-button>
         </el-row>
        </el-col>
      </el-row>
      <!-- 提示 -->
      <el-alert
      style="margin-bottom: 20px;"
        type="info"
        :closable="false"
        :title="`数据一共${counts}条`"
        show-icon>
        <!-- 数据一共{{counts}}条 -->
      </el-alert>
      <!-- 表格 -->
      <el-table :data="tableData" style="width: 100%">
      <el-table-column label="序号" type="index" align="center" width="70">
      </el-table-column>
      <el-table-column  label="文章标题"  >
        <template v-slot='{row}'>
         {{row.title}}
         <i class="el-icon-film"
          v-if="row.videoURL"
          @click="video(row.videoURL)"
          ></i>
          <!-- <video src=""></video> -->
        </template>
      </el-table-column>
      <el-table-column prop="visits" label="阅读数" align="center" width="80">
      </el-table-column>
      <el-table-column prop="username" label="录入人" align="center" width="150">
      </el-table-column>
      <el-table-column label="录入时间" align="center" width="200">
        <template v-slot="{row}">
          {{row.createTime | parseTimeByString()}}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" width="70">
        <template v-slot="{row}">
        {{row.state === 1 ? '已禁用':'已启用'}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template v-slot='{row}'>
        <el-button type="text" @click="previeweye(row)">预览</el-button>
        <el-button type="text" @click="disab(row)">{{row.state === 1 ? '启用':'禁用' }}</el-button>
        <el-button type="text" :disabled='row.state === 1' @click="modify(row.id)">修改</el-button>
        <el-button type="text" :disabled='row.state === 1' @click="del(row)">删除</el-button>

      </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-row type="flex" justify="end">
      <el-pagination
      style="margin-top:20px;"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="articles"
      layout=" prev, pager, next, sizes, jumper"
      background
      :total="counts">
    </el-pagination>
    </el-row>
    </el-card>
  <!-- 视频组件 -->
  <div class="box">
    <el-dialog
  :visible.sync="outerVisible"
  :modal='true'
  @close='close'
  >
    <video class="vid" ref="vid" v-viderror="'https://v-cdn.zjol.com.cn/277003.mp4'" :src="videoURL"></video>
  </el-dialog>
  </div>
  <!-- 预览 -->
  <ArticlesPreview :eye='eye' :preview.sync='preview'/>
  <!-- 添加 修改 -->
  <ArticlesAdd
  :add.sync='add'
  :id='id'
  />
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/articles'
import ArticlesPreview from '@/module-hmmm/components/articles-preview.vue'
import ArticlesAdd from '@/module-hmmm/components/articles-add.vue'
export default {
  components: {
    ArticlesPreview,
    ArticlesAdd
  },
  data () {
    return {
      // 表单
      formInline: {
        keyword: '',
        state: ''
      },
      // 总计录数
      counts: null,
      // 表格
      tableData: [],
      // 当前页数
      currentPage: 1,
      // 当前条数
      articles: 10,
      // 视频弹窗
      outerVisible: false,
      videoURL: '',
      eye: {},
      // 控制预览弹窗
      preview: false,
      // 控制修改和添加
      add: false,
      // 当前编辑的id
      id: null
    }
  },
  created () {
    this.list()
  },
  methods: {
    async list (page = 1, pagesize = 10) {
      const { data: res } = await list({ page, pagesize })
      console.log(res)
      const { counts, items } = res
      // 存总计录数
      this.counts = counts
      // console.log(this.counts)
      // 表格
      this.tableData = items
    },
    handleSizeChange (val) {
      this.articles = val
      this.list(this.currentPage, val)
    },
    // 下一页
    handleCurrentChange (val) {
      this.list(val, this.articles)
    },
    // 禁用 启用
    async disab (row) {
      // console.log(row)
      await changeState({
        id: row.id,
        state: row.state === 1 ? 0 : 1
      })
      // console.log(res)
      // 重新渲染
      this.$message.success('修改成功')
      this.list(this.currentPage, this.articles)
    },
    // 删除
    async del (row) {
      try {
        await this.$confirm('你确定要删除吗', '温馨提示', {
          type: 'warning'
        })
      } catch {
        return false
      }
      await remove(row)
      // 重新渲染
      this.$message.success('删除成功')
      this.list(this.currentPage, this.articles)
    },
    // 视频
    async video (videoURL) {
      this.outerVisible = true
      // console.log(videoURL)
      this.videoURL = videoURL
      await this.$nextTick()
      this.$refs.vid.play()
    },
    close () {
      this.$refs.vid.pause()// 停止播放
    },
    previeweye (row) {
      this.eye = row
      this.preview = true
      console.log(this.eye)
    },
    // 修改
    modify (id) {
      this.id = id
      this.add = true
    },
    // 添加
    newadd () {
      this.id = null
      this.add = true
    },
    clear () {
      this.formInline = {
        keyword: '',
        state: ''
      }
      this.list() // 恢复原来的样子
    },
    // 搜索
    async search () {
      const { data } = await list(this.formInline)
      console.log(data)
      this.counts = data.counts
      this.tableData = data.items
    }
  }

}
</script>

<style lang="less" scoped>

.container{
  padding:10px 5px 0 5px;
}
.el-icon-film{
  font-size: 20px;
  margin-left: 5px;
  color:blue;
}
.box{
      ::v-deep{
      .el-dialog__header{
        background:#fff;
        padding: 0;
        height: 0;
        width: 0;
        position:relative;

          .el-dialog__close{
            position:absolute;
            background:black;
            height: 50px;
            width: 50px;
            top: -100px;
            left: 370px;
            font-size: 30px;
            color: #fff;
            line-height: 50px;
            border-radius: 25px;
            z-index: 999;

          }

    }
}
  ::v-deep{
    .el-dialog__body{
          padding: 0;
          opacity: 1;
          border:0;
    }
  }
}
.vid{
  width: 100%;
  height: 100%;
  transform: scale(102%);
}
</style>
